﻿/* *
 * =============================================================================
 * File Name:               Page.xaml.cs
 * Programmer:              Ken Watts
 * Creation Date:           10/20/2008
 * =============================================================================
 * Description:             Class that represents a Page.xaml.cs
 * =============================================================================
 * Modification Date:       10/20/2008 - Initial Revision
 * =============================================================================
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml;
using System.Xml.Linq;

namespace SlideShowViewer
{
    public partial class Page : UserControl
    {
        private const int k_int_default_width = 320;
        private const int k_int_default_height = 240;
        private const int k_int_default_image_interval_secs = 5;
        private const string k_str_xml_image_list = "ImageList.xml";
        private const string k_str_element_image = "Image";
        private const string k_str_element_image_list = "ImageList";
        private const string k_str_attribute_width = "Width";
        private const string k_str_attribute_height = "Height";
        private const string k_str_attribute_shuffle = "Shuffle";
        private const string k_str_use_shuffle = "yes";

        private SlideShow mySlideShow;
        private DispatcherTimer timer = new DispatcherTimer();

        public Page()
        {
            InitializeComponent();
        }

        protected virtual void CustomMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Display the next image
            mySlideShow.Next();
            // Restart the timer
            timer.Stop();
            timer.Start();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            XDocument doc = XDocument.Load(k_str_xml_image_list);
            int width, height; 
            bool shuffle;
            
            // Determine if the width has been defined as an attribute
            try
            {
                width = Convert.ToInt32(doc.Element(k_str_element_image_list).Attribute(k_str_attribute_width).Value);
            }
            catch (Exception) 
            {
                width = k_int_default_width;
            }
            // Determine if the height has been defined as an attribute
            try
            {
                height = Convert.ToInt32(doc.Element(k_str_element_image_list).Attribute(k_str_attribute_height).Value);
            }
            catch (Exception) 
            {
                height = k_int_default_height;
            }
            // Set the width and height
            LayoutRoot.Width = width;
            LayoutRoot.Height = height;

            // Determine if shuffling is being used
            shuffle = (doc.Element(k_str_element_image_list).Attribute(k_str_attribute_shuffle).Value.ToLower() == k_str_use_shuffle);

            // Initialize the slide show viewer
            mySlideShow = new SlideShow(LayoutRoot,width,height);

            // Load image file names
            IEnumerable<XElement> images = doc.Element(k_str_element_image_list).Elements(k_str_element_image);
            foreach (XElement image in images)
            {
                string imageFileName = image.Value;
                mySlideShow.AddImage(imageFileName);
            }

            // Setup a custom mouse button handler
            this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomMouseLeftButtonDown);

            // Start the slide show
            mySlideShow.BeginShow(shuffle);

            // Initialze the timer
            timer.Interval = new TimeSpan(0, 0, 0, k_int_default_image_interval_secs, 0);
            timer.Tick += new EventHandler(TimerTick);
            timer.Start();
        }

        void TimerTick(object sender, EventArgs e)
        {
            // Display the next image
            mySlideShow.Next();
        }
    }
}
